package com.example.demo01.picture_frame.frame;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;

/**
 * <p>
 *
 * </p>
 *
 * @author DONG.WANG17@HAND-CHINA.COM
 * @since 2024-11-10 21:17
 */
public abstract class BaseFrame extends View {
    public BaseFrame(Context context) {
        super(context);
    }
    /**
     * 根据圈定的宽高（px），将缩放Bitmap到适合大小
     *
     * @param scopeW 视图的宽度
     * @param scopeH 视图的高度
     * @param bitmap 要缩放的  Bitmap
     */
    protected Bitmap scaleBitmapToFit(int scopeW, int scopeH, Bitmap bitmap) {
        if (bitmap == null) return null;
        // 假设图片的宽高比大于视图的宽高比，则按宽度缩放，否则按高度缩放
        float aspectRatio = (float) bitmap.getWidth() / bitmap.getHeight();
        if (aspectRatio > 1) {
            return Bitmap.createScaledBitmap(bitmap, scopeW, (int) (scopeW / aspectRatio), true);
        } else {
            return Bitmap.createScaledBitmap(bitmap, (int) (scopeH * aspectRatio), scopeH, true);
        }
    }

    public abstract void setBitmap(Bitmap bitmap);
}
